﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using nkstdgia.common.ThamDinhGia.BusinessObjects;

namespace nkstdgia.common.ThamDinhGia.DataAccess
{
    public class THAM_SODA
    {
        #region ***** Init Methods *****

        public THAM_SODA()
        {
        }

        #endregion ***** Init Methods *****

        #region ***** Get Methods *****

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public THAM_SO Populate(IDataReader myReader)
        {
            THAM_SO obj = new THAM_SO();
            obj.MA_THAM_SO = myReader["MA_THAM_SO"] != DBNull.Value ? (Int64)myReader["MA_THAM_SO"] : -1;
            obj.PARENT_ID = myReader["PARENT_ID"] != DBNull.Value ? (Int64)myReader["PARENT_ID"] : -1;
            obj.LOAI_THAM_SO = myReader["LOAI_THAM_SO"] != DBNull.Value ? (string)myReader["LOAI_THAM_SO"] : "";
            obj.NOI_DUNG = myReader["NOI_DUNG"] != DBNull.Value ? (string)myReader["NOI_DUNG"] : "";
            obj.SEQUENCE = myReader["SEQUENCE"] != DBNull.Value ? (int)myReader["SEQUENCE"] : -1;
            obj.GHI_CHU = myReader["GHI_CHU"] != DBNull.Value ? (string)myReader["GHI_CHU"] : "";
            return obj;
        }

        /// <summary>
        /// Get THAM_SO by ma_tham_so
        /// </summary>
        /// <param name="ma_tham_so">MA_THAM_SO</param>
        /// <returns>THAM_SO</returns>
        public THAM_SO GetByMA_THAM_SO(Int64 ma_tham_so)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_GetByMA_THAM_SO", Data.CreateParameter("MA_THAM_SO", ma_tham_so)))
            {
                if (reader.Read())
                {
                    return Populate(reader);
                }
                return null;
            }
        }

        /// <summary>
        /// Get all of THAM_SO
        /// </summary>
        /// <returns>List<<THAM_SO>></returns>
        public List<THAM_SO> GetList()
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_Get"))
            {
                List<THAM_SO> list = new List<THAM_SO>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of THAM_SO
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet()
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_Get");
        }

        /// <summary>
        /// Get all of THAM_SO paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>List<<THAM_SO>></returns>
        public List<THAM_SO> GetListPaged(int recperpage, int pageindex)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex)))
            {
                List<THAM_SO> list = new List<THAM_SO>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of THAM_SO paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetPaged(int recperpage, int pageindex)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex));
        }

        #endregion ***** Get Methods *****

        #region ***** Add Update Delete Methods *****

        /// <summary>
        /// Add a new THAM_SO within THAM_SO database table
        /// </summary>
        /// <param name="obj">THAM_SO</param>
        /// <returns>key of table</returns>
        public long Add(THAM_SO obj)
        {
            DbParameter parameterItemID = Data.CreateParameter("MA_THAM_SO", obj.MA_THAM_SO);
            parameterItemID.Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_Add"
                            , parameterItemID
                            , Data.CreateParameter("PARENT_ID", obj.PARENT_ID)
                            , Data.CreateParameter("LOAI_THAM_SO", obj.LOAI_THAM_SO)
                            , Data.CreateParameter("NOI_DUNG", obj.NOI_DUNG)
                            , Data.CreateParameter("SEQUENCE", obj.SEQUENCE)
                            , Data.CreateParameter("GHI_CHU", obj.GHI_CHU)
            );
            return long.Parse(parameterItemID.Value.ToString());
        }

        /// <summary>
        /// updates the specified THAM_SO
        /// </summary>
        /// <param name="obj">THAM_SO</param>
        /// <returns></returns>
        public void Update(THAM_SO obj)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_Update"
                            , Data.CreateParameter("PARENT_ID", obj.MA_THAM_SO)
                            , Data.CreateParameter("MA_THAM_SO", obj.MA_THAM_SO)
                            , Data.CreateParameter("LOAI_THAM_SO", obj.LOAI_THAM_SO)
                            , Data.CreateParameter("NOI_DUNG", obj.NOI_DUNG)
                            , Data.CreateParameter("SEQUENCE", obj.SEQUENCE)
                            , Data.CreateParameter("GHI_CHU", obj.GHI_CHU)
            );
        }

        /// <summary>
        /// Delete the specified THAM_SO
        /// </summary>
        /// <param name="ma_tham_so">MA_THAM_SO</param>
        /// <returns></returns>
        public void Delete(Int64 ma_tham_so)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_THAM_SO_Delete", Data.CreateParameter("MA_THAM_SO", ma_tham_so));
        }

        #endregion ***** Add Update Delete Methods *****
    }
}